package proxy.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
    //维护一个目标对象
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }
    //为目标对象生成代理对象
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(
                //为目标对象生成代理对象
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if ("save".equals(method.getName())) {
                            System.out.println("事务开始");
                            System.out.println("0 paras="+args[0]);
//                          System.out.println("1 paras="+args[1]);
                            int bigage = (int)args[0];
                            method.invoke(target,2*bigage);
                            System.out.println("事务提交====");
                        }
                        return null;
                    }
                }
        );
    }
}
